<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>正则回顾总结 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,正则表达式,">
  

  <meta name="description" content="一、简介1.1 正则表达式的web常见场合 邮箱验证 用户名验证 替换字符串某一部分 信息采集 用来分析有效的代码段  1.2 字符串应用正则 string.match(reg)正则查找字符串  string.search(reg) 正则查找位置 string.replace(reg,&amp;#39;newstr&amp;#39;)正则替换 string.split（reg）正则拆分  1.3 正则表达式3句话">
<meta name="keywords" content="JavaScript,正则表达式">
<meta property="og:type" content="article">
<meta property="og:title" content="正则回顾总结">
<meta property="og:url" content="http://blog.poetries.top/2018/04/10/js-reg-review/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、简介1.1 正则表达式的web常见场合 邮箱验证 用户名验证 替换字符串某一部分 信息采集 用来分析有效的代码段  1.2 字符串应用正则 string.match(reg)正则查找字符串  string.search(reg) 正则查找位置 string.replace(reg,&amp;#39;newstr&amp;#39;)正则替换 string.split（reg）正则拆分  1.3 正则表达式3句话">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://7xq6al.com1.z0.glb.clouddn.com/JavaScript%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.gif">
<meta property="og:updated_time" content="2020-08-15T04:25:31.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="正则回顾总结">
<meta name="twitter:description" content="一、简介1.1 正则表达式的web常见场合 邮箱验证 用户名验证 替换字符串某一部分 信息采集 用来分析有效的代码段  1.2 字符串应用正则 string.match(reg)正则查找字符串  string.search(reg) 正则查找位置 string.replace(reg,&amp;#39;newstr&amp;#39;)正则替换 string.split（reg）正则拆分  1.3 正则表达式3句话">
<meta name="twitter:image" content="http://7xq6al.com1.z0.glb.clouddn.com/JavaScript%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.gif">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、简介"><span class="toc-text">一、简介</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-正则表达式的web常见场合"><span class="toc-text">1.1 正则表达式的web常见场合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-字符串应用正则"><span class="toc-text">1.2 字符串应用正则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-正则表达式3句话"><span class="toc-text">1.3 正则表达式3句话</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-1-要找什么字符？（5中方法）"><span class="toc-text">1.3.1 要找什么字符？（5中方法）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-2-从哪里找-找到哪？"><span class="toc-text">1.3.2 从哪里找,找到哪？</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-3-找多少"><span class="toc-text">1.3.3 找多少</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-贪婪模式"><span class="toc-text">1.3 贪婪模式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、语法"><span class="toc-text">二、语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-基本元字符"><span class="toc-text">2.1 基本元字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-数量元字符"><span class="toc-text">2.2 数量元字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-位置元字符"><span class="toc-text">2.3 位置元字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-特殊元字符"><span class="toc-text">2.4 特殊元字符</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-标志字符"><span class="toc-text">2.5 标志字符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、match，test，exec，search的返回值"><span class="toc-text">三、match，test，exec，search的返回值</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-match的用法"><span class="toc-text">3.1 match的用法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-exec的用法"><span class="toc-text">3.2 exec的用法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-test的用法"><span class="toc-text">3.3 test的用法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-search的用法"><span class="toc-text">3.4 search的用法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、应用"><span class="toc-text">四、应用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-例1"><span class="toc-text">4.1 例1</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-例2-要找什么字符串"><span class="toc-text">4.2 例2-要找什么字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-例3-从哪里开始匹配"><span class="toc-text">4.3 例3-从哪里开始匹配</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-例4-正则应用字符串"><span class="toc-text">4.4 例4 正则应用字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-例5-预查"><span class="toc-text">4.5 例5-预查</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-6-例6-反向引用"><span class="toc-text">4.6 例6-反向引用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-7-例7-正则练习"><span class="toc-text">4.7 例7-正则练习</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-8-例8-常用正则"><span class="toc-text">4.8 例8-常用正则</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五、总结"><span class="toc-text">五、总结</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六、扩展阅读"><span class="toc-text">六、扩展阅读</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-js-reg-review" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">正则回顾总结</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.04.10</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 4.6k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 22分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、简介"><a href="#一、简介" class="headerlink" title="一、简介"></a>一、简介</h2><h3 id="1-1-正则表达式的web常见场合"><a href="#1-1-正则表达式的web常见场合" class="headerlink" title="1.1 正则表达式的web常见场合"></a>1.1 正则表达式的web常见场合</h3><ul>
<li>邮箱验证</li>
<li>用户名验证</li>
<li>替换字符串某一部分</li>
<li>信息采集 用来分析有效的代码段</li>
</ul>
<h3 id="1-2-字符串应用正则"><a href="#1-2-字符串应用正则" class="headerlink" title="1.2 字符串应用正则"></a>1.2 字符串应用正则</h3><ul>
<li><code>string.match(reg)</code>正则查找字符串 </li>
<li><code>string.search(reg)</code> 正则查找位置</li>
<li><code>string.replace(reg,&#39;newstr&#39;)</code>正则替换</li>
<li><code>string.split（reg）</code>正则拆分</li>
</ul>
<h3 id="1-3-正则表达式3句话"><a href="#1-3-正则表达式3句话" class="headerlink" title="1.3 正则表达式3句话"></a>1.3 正则表达式3句话</h3><h4 id="1-3-1-要找什么字符？（5中方法）"><a href="#1-3-1-要找什么字符？（5中方法）" class="headerlink" title="1.3.1 要找什么字符？（5中方法）"></a>1.3.1 要找什么字符？（5中方法）</h4><ul>
<li>字面值 比如：<code>hi</code>就是找<code>hi</code></li>
<li>字符的集合<code>[abcd]</code>匹配<code>abdc</code>任意一个</li>
<li>用范围来表示字符<code>[0-9] [a-z][A-Z]</code></li>
<li>字符簇 就是系统为常用的字符集合创建的一个简写<ul>
<li>例如：<code>\d</code> 表示<code>[0-9]</code> <code>\w</code>代表<code>[0-9a-zA-Z_]</code>  <code>\s</code>代表<code>[\t\v\f\r\n]</code>（空白符）</li>
</ul>
</li>
<li>补集的形式来表示字符集合<ul>
<li>例如：<code>[0-9]</code> 补集 <code>[^0-9]</code></li>
<li>对于字符簇只需把字母大写即可表示补集 <code>\d</code> –&gt;<code>\D</code>（非数字）  <code>\w</code>–&gt;<code>\W</code> （代表<code>a-z0-9A-Z</code>之外的东西） <code>\s</code> –&gt;<code>\S</code>(代表非空白字符)</li>
</ul>
</li>
<li>点代表 任意字符 (不包括换行符)</li>
</ul>
<h4 id="1-3-2-从哪里找-找到哪？"><a href="#1-3-2-从哪里找-找到哪？" class="headerlink" title="1.3.2 从哪里找,找到哪？"></a>1.3.2 从哪里找,找到哪？</h4><ul>
<li><code>\b</code>单词边界  <code>\bhi</code>从单词边界开始匹配<code>hi</code></li>
<li><code>\B</code> 单词的非边界 把单词的中间某一部分取出来  把中间含有hi的单词取出来 即<code>hi</code>不能再两端</li>
<li><code>^</code> 从字符串的起始位置开始匹配</li>
<li><code>$</code>匹配到字符串的额结束位置</li>
</ul>
<h4 id="1-3-3-找多少"><a href="#1-3-3-找多少" class="headerlink" title="1.3.3 找多少"></a>1.3.3 找多少</h4><ul>
<li><code>*</code>表示匹配<code>0-</code>无穷<code>==</code>等价<code>(0,)</code></li>
<li><code>+</code>表示匹配<code>1-</code>无穷 <code>==</code>等价<code>(1,)</code></li>
<li><code>?</code>表示<code>[0,1] ==</code> 等价<code>(0,1)</code></li>
<li><code>a(n)</code>: 字符a准确出<code>现n</code>次</li>
<li><code>a(n,)</code>:字符<code>a</code>至少出现<code>n</code>次</li>
<li><code>a(n,m):n</code>到<code>m</code>次</li>
</ul>
<h3 id="1-3-贪婪模式"><a href="#1-3-贪婪模式" class="headerlink" title="1.3 贪婪模式"></a>1.3 贪婪模式</h3><ul>
<li>个数修饰符默认是贪婪模式 尽量多找</li>
<li>是指在上面的个数修饰符后面加<code>?</code>，则为非贪婪模式，尽量少的找</li>
</ul>
<p><strong>模式</strong>：</p>
<ul>
<li>以匹配为例默认情况 match找到一次就结束 能否告诉匹配过程 一直找 在全文范围内一直找</li>
<li><code>g</code> 表示全局模式 global  找所有的  而不是就找一行就结束</li>
<li><code>i</code> ignore 忽略大小写</li>
<li><code>s</code> 单行模式（把整篇文章看成一行）js不支持单行模式</li>
<li><code>m</code> 多行模式</li>
</ul>
<h2 id="二、语法"><a href="#二、语法" class="headerlink" title="二、语法"></a>二、语法</h2><h3 id="2-1-基本元字符"><a href="#2-1-基本元字符" class="headerlink" title="2.1 基本元字符"></a>2.1 基本元字符</h3><ul>
<li><code>.</code> ： 匹配除了换行符之外的任何单个字符</li>
<li><code>\</code> ： 在非特殊字符之前的反斜杠表示下一个字符是特殊的</li>
<li><code>|</code>： 逻辑或操作符</li>
<li><code>[]</code> ：定义一个字符集合，匹配字符集合中的一个字符，在字符集合里面像 <code>.</code>，\这些字符都表示其本身</li>
<li><code>[^]</code>：对上面一个集合取非</li>
<li><code>-</code>：定义一个区间，例如<code>[A-Z]</code>，其首尾字符在 <code>ASCII</code> 字符集里面</li>
</ul>
<h3 id="2-2-数量元字符"><a href="#2-2-数量元字符" class="headerlink" title="2.2 数量元字符"></a>2.2 数量元字符</h3><ul>
<li><code>{m,n}</code> ：匹配前面一个字符至少 <code>m</code> 次至多 <code>n</code> 次重复，还有<code>{m}</code>表示匹配 <code>m</code> 次，<code>{m,}</code>表示至少 <code>m</code> 次</li>
<li><code>+</code> ： 匹配前面一个表达式一次或者多次，相当于<code>{1,}</code>，记忆方式追加<code>(+)</code>，起码得有一次</li>
<li><code>*</code> ： 匹配前面一个表达式零次或者多次，相当于<code>{0,}</code>，记忆方式乘法<code>(*)</code>，可以一次都没有</li>
<li><code>?</code> ： 单独使用匹配前面一个表达式零次或者一次，相当于 <code>{0,1}</code></li>
</ul>
<h3 id="2-3-位置元字符"><a href="#2-3-位置元字符" class="headerlink" title="2.3 位置元字符"></a>2.3 位置元字符</h3><ul>
<li><code>^</code> ： 单独使用匹配表达式的开始</li>
<li><code>\$</code>： 匹配表达式的结束</li>
<li><code>\b</code>：匹配单词边界</li>
<li><code>\B</code>：匹配非单词边界</li>
<li><code>(?=p)</code>：匹配 <code>p</code> 前面的位置</li>
<li><code>(?!p)</code>：匹配不是 <code>p</code> 前面的位置</li>
</ul>
<h3 id="2-4-特殊元字符"><a href="#2-4-特殊元字符" class="headerlink" title="2.4 特殊元字符"></a>2.4 特殊元字符</h3><ul>
<li><code>\d</code>：<code>[0-9]</code>，表示一位数字，记忆方式 digit</li>
<li><code>\D</code>：<code>[^0-9]</code>，表示一位非数字</li>
<li><code>\s</code>：<code>[\t\v\n\r\f]</code>，表示空白符，包括空格，水平制表符（\t），垂直制表符（\v），换行符（\n），回车符（\r），换页符（\f），记忆方式 space character</li>
<li><code>\S</code>：<code>[^\t\v\n\r\f]</code>，表示非空白符</li>
<li><code>\w</code>：<code>[0-9a-zA-Z]</code>，表示数字大小写字母和下划线，记忆方式 word</li>
<li><code>\W</code>：<code>[^0-9a-zA-Z]</code>，表示非单词字符</li>
</ul>
<h3 id="2-5-标志字符"><a href="#2-5-标志字符" class="headerlink" title="2.5 标志字符"></a>2.5 标志字符</h3><ul>
<li><code>g</code> : 全局搜索 记忆方式global</li>
<li><code>i</code> ：不区分大小写 记忆方式 ignore</li>
<li><code>m</code> ：多行搜索</li>
</ul>
<h2 id="三、match，test，exec，search的返回值"><a href="#三、match，test，exec，search的返回值" class="headerlink" title="三、match，test，exec，search的返回值"></a>三、match，test，exec，search的返回值</h2><h3 id="3-1-match的用法"><a href="#3-1-match的用法" class="headerlink" title="3.1 match的用法"></a>3.1 match的用法</h3><p><strong>在不加全局“g”的情况下</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">"wo shi zhong guo ren"</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(str.match(<span class="regexp">/o/</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 返回的结果是：["o", index: 1, input: "wo shi zhong guo ren"]；</span></span><br></pre></td></tr></table></figure>
<p><strong>在加全局“g”的情况下</strong></p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">"wo shi zhong guo ren"</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(str.match(<span class="regexp">/o/g</span>));</span><br><span class="line"></span><br><span class="line">返回的结果是：[<span class="string">"o"</span>, <span class="string">"o"</span>, <span class="string">"o"</span>]；</span><br></pre></td></tr></table></figure>
<h3 id="3-2-exec的用法"><a href="#3-2-exec的用法" class="headerlink" title="3.2 exec的用法"></a>3.2 exec的用法</h3><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">"wo shi zhong guo ren"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> re=<span class="regexp">/o/</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(re.exec(str));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 返回的结果是：["o", index: 1, input: "wo shi zhong guo ren"]；</span></span><br><span class="line"><span class="comment">// exec返回的和match不加全局“g”’一样</span></span><br></pre></td></tr></table></figure>
<h3 id="3-3-test的用法"><a href="#3-3-test的用法" class="headerlink" title="3.3 test的用法"></a>3.3 test的用法</h3><figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">"wo shi zhong guo ren"</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> re=<span class="regexp">/o/</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(re.test(str));</span><br><span class="line"></span><br><span class="line"><span class="comment">// 返回的结果是：true（如果找不到返回的则是false）</span></span><br></pre></td></tr></table></figure>
<h3 id="3-4-search的用法"><a href="#3-4-search的用法" class="headerlink" title="3.4 search的用法"></a>3.4 search的用法</h3><ul>
<li>关于search用法其实很简单，它和indexOf是一样的，就是找到index，而且是从前往后数的。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str=<span class="string">"wo shi zhong guo ren"</span>;</span><br><span class="line"><span class="keyword">var</span> re=<span class="regexp">/o/</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(str.search(re));</span><br><span class="line"></span><br><span class="line">返回的结果是：<span class="number">1</span>；</span><br></pre></td></tr></table></figure>
<h2 id="四、应用"><a href="#四、应用" class="headerlink" title="四、应用"></a>四、应用</h2><h3 id="4-1-例1"><a href="#4-1-例1" class="headerlink" title="4.1 例1"></a>4.1 例1</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/xWMRxR" target="_blank" rel="noopener">https://codepen.io/poetries/pen/xWMRxR</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>正则-例子1<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span></span><br><span class="line">		textarea&#123;</span><br><span class="line">			width: 400px;</span><br><span class="line">			height: 200px; </span><br><span class="line">		&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配hi"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"正匹配单词hi"</span> <span class="attr">onclick</span>=<span class="string">"t2();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配hi开头的单词但不是hi"</span> <span class="attr">onclick</span>=<span class="string">"t3();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/hi/</span>;<span class="comment">//仅仅是看字符串中有没有hi</span></span></span><br><span class="line"><span class="javascript">            	alert(reg.test(cv));<span class="comment">//满足 返回true  不满足 返回false</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t2</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\bhi\b/</span>;<span class="comment">//正则测试单词hi</span></span></span><br><span class="line"><span class="javascript">            	alert(reg.test(cv));<span class="comment">//满足 返回true  不满足 返回false</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t3</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\bhi\b/</span>;<span class="comment">//正则测试hi开头的单词但不是hi</span></span></span><br><span class="line"><span class="javascript">            	alert(reg.test(cv));<span class="comment">//满足 返回true  不满足 返回false</span></span></span><br><span class="line">            &#125; </span><br><span class="line">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-2-例2-要找什么字符串"><a href="#4-2-例2-要找什么字符串" class="headerlink" title="4.2 例2-要找什么字符串"></a>4.2 例2-要找什么字符串</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/oqmYNw" target="_blank" rel="noopener">https://codepen.io/poetries/pen/oqmYNw</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>要找什么字符<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span></span><br><span class="line">    	textarea&#123;</span><br><span class="line">    	width: 400px;</span><br><span class="line">    	height: 200px; </span><br><span class="line">    	&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"找不好的数字"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"有没有数字"</span> <span class="attr">onclick</span>=<span class="string">"t2();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"有没有大写字母"</span> <span class="attr">onclick</span>=<span class="string">"t3();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"是否全为数字"</span> <span class="attr">onclick</span>=<span class="string">"t4();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/[347]/</span>;<span class="comment">// 匹配347任意一个字符的集合[abcd] 匹配abdc任意一个</span></span></span><br><span class="line">            	alert(reg.test(cv));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t2</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="comment">//var reg = /[0123456789]/;</span></span></span><br><span class="line"><span class="javascript">            	<span class="comment">//var reg = /[0-9]/;// 匹配有没有数字</span></span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\d/</span>;<span class="comment">// \d 表示[0-9]</span></span></span><br><span class="line">            	alert(reg.test(cv));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t3</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/[A-Z]/</span>;<span class="comment">// 匹配有没有数字</span></span></span><br><span class="line">            	alert(reg.test(cv));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t4</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/[^0-9]/</span>;<span class="comment">// 匹配非数字 0-9之外的字符  对于网上发布手机号之类的很有用 比如1300 把00换成OO 就没法认出</span></span></span><br><span class="line">            	if(reg.test(cv))&#123;</span><br><span class="line"><span class="javascript">            		alert(<span class="string">'有非数字存在'</span>);</span></span><br><span class="line"><span class="javascript">            	&#125;<span class="keyword">else</span>&#123;</span></span><br><span class="line"><span class="javascript">            		alert(<span class="string">'全是数字'</span>);</span></span><br><span class="line">            	&#125;</span><br><span class="line">            &#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-3-例3-从哪里开始匹配"><a href="#4-3-例3-从哪里开始匹配" class="headerlink" title="4.3 例3-从哪里开始匹配"></a>4.3 例3-从哪里开始匹配</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/jzdVOo" target="_blank" rel="noopener">https://codepen.io/poetries/pen/jzdVOo</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>74-从哪里开始匹配<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span></span><br><span class="line">		textarea&#123;</span><br><span class="line">			width: 400px;</span><br><span class="line">			height: 200px;</span><br><span class="line">			</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配hi"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配hi开头及hi开头的单词"</span> <span class="attr">onclick</span>=<span class="string">"t2();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配hi开头的单词但不是hi"</span> <span class="attr">onclick</span>=<span class="string">"t3();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配进行时的单词"</span> <span class="attr">onclick</span>=<span class="string">"t4();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配un前缀的反义词"</span> <span class="attr">onclick</span>=<span class="string">"t5();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配单词中间的hi部分"</span> <span class="attr">onclick</span>=<span class="string">"t6();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配输入的名字是不是lisi"</span> <span class="attr">onclick</span>=<span class="string">"t7();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/hi/</span>;<span class="comment">//仅仅是看字符串中有没有hi</span></span></span><br><span class="line"><span class="javascript">            	alert(reg.test(cv));<span class="comment">//满足 返回true  不满足 返回false</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t2</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="comment">//var reg = /\bhi.+/;// 错误1 匹配hi开头的单词</span></span></span><br><span class="line"><span class="javascript">            	<span class="comment">////var reg = /\bhi\w+/;// 错误2</span></span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\bhi\w*/</span>; </span></span><br><span class="line"><span class="javascript">            	alert(reg.exec(cv));<span class="comment">//exec返回一个对象  没找到返回none</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t3</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\bhi/</span>;<span class="comment">//正则测试hi开头的单词但不是hi</span></span></span><br><span class="line"><span class="javascript">            	alert(reg.exec(cv));<span class="comment">//exec返回一个对象  没找到返回none</span></span></span><br><span class="line">            &#125; </span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t4</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\b[\w]+ing\b/</span>;<span class="comment">//*表示匹配0-无穷  +表示匹配1-无穷</span></span></span><br><span class="line">            	alert(reg.exec(cv));</span><br><span class="line">            &#125; </span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t5</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\bun[\w]+\b/</span>;<span class="comment">//匹配un前缀的反义词</span></span></span><br><span class="line">            	alert(reg.exec(cv));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t6</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\Bhi\B/</span>;<span class="comment">//匹配单词中间的hi部分</span></span></span><br><span class="line">            	alert(reg.exec(cv));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t7</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/^lisi$/</span>;<span class="comment">//匹配输入的名字是不是lisi</span></span></span><br><span class="line">            	alert(reg.exec(cv));</span><br><span class="line">            &#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-4-例4-正则应用字符串"><a href="#4-4-例4-正则应用字符串" class="headerlink" title="4.4 例4 正则应用字符串"></a>4.4 例4 正则应用字符串</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/dmaOPz" target="_blank" rel="noopener">https://codepen.io/poetries/pen/dmaOPz</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>正则应用字符串<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span></span><br><span class="line">		textarea&#123;</span><br><span class="line">			width: 400px;</span><br><span class="line">			height: 200px; </span><br><span class="line">		&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"查找中间含有hi的单词"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"查找所有中间含有hi的单词"</span> <span class="attr">onclick</span>=<span class="string">"t2();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"查找所有中间含有hi的单词，不区分大小写"</span> <span class="attr">onclick</span>=<span class="string">"t3();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"替换JavaScript标签"</span> <span class="attr">onclick</span>=<span class="string">"t4();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"把连接换成空连接"</span> <span class="attr">onclick</span>=<span class="string">"t5();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"把每一行的结尾的数字换成#"</span> <span class="attr">onclick</span>=<span class="string">"t6();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"替换goods中多余的O"</span> <span class="attr">onclick</span>=<span class="string">"t7();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\Bhi\B/</span>;<span class="comment">//查找中间含有hi的单词</span></span></span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t2</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\Bhi\B/g</span>;<span class="comment">//查找所有中间含有hi的单词</span></span></span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t3</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\Bhi\B/gi</span>;<span class="comment">//查找所有中间含有hi的单词  g是全局 i ignore 忽略大小写</span></span></span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t4</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span>  reg = <span class="regexp">/&lt;script.*&lt;\/script&gt;/</span>;<span class="comment">//替换JavaScript标签</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">'哈哈哈'</span>));</span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t5</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line">            	  /*</span><br><span class="line">                        . 代表任意,但不包括换行</span><br><span class="line">                        可以用一对反义词来匹配所有. \d\D等</span><br><span class="line">                        不能跨行（贪婪模式）</span><br><span class="line">                  */</span><br><span class="line">                 </span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/&lt;a[\s]+[\d\D]*&lt;\/a&gt;/</span>; <span class="comment">//正则替换链接</span></span></span><br><span class="line"><span class="xml">                    alert(cv.replace(reg,'<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span>&gt;</span>文字<span class="tag">&lt;/<span class="name">a</span>&gt;</span>')); </span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t6</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span>  reg = <span class="regexp">/\d+$/gm</span>;<span class="comment">//把每一行的结尾的数字换成#   m 多行模式</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">'#'</span>));</span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t7</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span>  reg = <span class="regexp">/go&#123;3,&#125;?ds/</span>;<span class="comment">//替换goods中多余的O</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">'goods'</span>));</span></span><br><span class="line">            &#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-5-例5-预查"><a href="#4-5-例5-预查" class="headerlink" title="4.5 例5-预查"></a>4.5 例5-预查</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/MVLbwW" target="_blank" rel="noopener">https://codepen.io/poetries/pen/MVLbwW</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>预查<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span></span><br><span class="line">		textarea&#123;</span><br><span class="line">			width: 400px;</span><br><span class="line">			height: 200px; </span><br><span class="line">		&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配进行时的单词"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"匹配进行时的单词的词根部分,正向预查"</span> <span class="attr">onclick</span>=<span class="string">"t2();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"查找winxp,负向预查"</span> <span class="attr">onclick</span>=<span class="string">"t3();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"找出un*系列单词的词根"</span> <span class="attr">onclick</span>=<span class="string">"t4();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\b[\w]+ing\b/</span>;</span></span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125; </span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t2</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\b[\w]+(?=ing)/g</span>;<span class="comment">//匹配进行时的单词的词根部分  不要ing结尾  预查不消耗字符</span></span></span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125; </span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t3</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\bwin(?!95)/g</span>;<span class="comment">//win98 win97 win95 win32 winxp win2003要求把win95过滤</span></span></span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125; </span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t4</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="comment">//var reg = /\w+(?&lt;=un)/g;//找出un*系列单词的词根  js不支持向前正向预查</span></span></span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125; </span><br><span class="line"> </span><br><span class="line">	<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-6-例6-反向引用"><a href="#4-6-例6-反向引用" class="headerlink" title="4.6 例6-反向引用"></a>4.6 例6-反向引用</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/LdqbVL" target="_blank" rel="noopener">https://codepen.io/poetries/pen/LdqbVL</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>反向引用<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span></span><br><span class="line">		textarea&#123;</span><br><span class="line">			width: 400px;</span><br><span class="line">			height: 200px; </span><br><span class="line">		&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"把连接换成空连接，保留文字"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="xml">            	var reg = /<span class="tag">&lt;<span class="name">a[\s]+[^</span>&gt;</span>]+&gt;([^<span class="tag">&lt;&gt;</span>]+)<span class="tag">&lt;<span class="name">\</span>/<span class="attr">a</span>&gt;</span>/; //链接表达式  这个一部分是子表达式 ([^<span class="tag">&lt;&gt;</span>]+)</span></span><br><span class="line"><span class="javascript">                    <span class="comment">// alert(reg.exec(cv)); </span></span></span><br><span class="line">                    </span><br><span class="line"><span class="xml">                alert(cv.replace(reg,'<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#"</span>"&gt;</span>$1<span class="tag">&lt;/<span class="name">a</span>&gt;</span>'));</span></span><br><span class="line">            &#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-7-例7-正则练习"><a href="#4-7-例7-正则练习" class="headerlink" title="4.7 例7-正则练习"></a>4.7 例7-正则练习</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/dmaOYV" target="_blank" rel="noopener">https://codepen.io/poetries/pen/dmaOYV</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>正则练习<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">"text/css"</span>&gt;</span></span><br><span class="line">		textarea&#123;</span><br><span class="line">			width: 400px;</span><br><span class="line">			height: 200px; </span><br><span class="line">		&#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"1-找首尾相同的单词"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"2-手机号第4位到第七位换成*"</span> <span class="attr">onclick</span>=<span class="string">"t2();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"3-统一空格"</span> <span class="attr">onclick</span>=<span class="string">"t3();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"4-把名字中的,去掉"</span> <span class="attr">onclick</span>=<span class="string">"t4();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"5-把aaabb换成ab"</span> <span class="attr">onclick</span>=<span class="string">"t5();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">           <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/\b([\w])[\w]*\1\b/g</span>;<span class="comment">// \1注意引用方式 [\w]任意一个 [\w]*任意多个 找首尾相同的单词</span></span></span><br><span class="line">            	/**</span><br><span class="line">            	 *  *如何引用子表达式所匹配的结果？</span><br><span class="line">             *</span><br><span class="line">             * 答： 在表达式内部 反向引用时 \n来匹配第N个子表达式的结果 子表达式当成变量来传递时，$N来匹配第N个子表达式的匹配结果</span><br><span class="line">             * </span><br><span class="line">            	 */</span><br><span class="line">            	alert(cv.match(reg));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t2</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/(1(3|5|8|)\d)\d&#123;4&#125;(\d&#123;4&#125;)/</span>;<span class="comment">//字表达式(3|5|8|)  (\d&#123;4&#125;)  &#123;4&#125;代表任意4个  </span></span></span><br><span class="line"><span class="javascript">            	<span class="comment">// alert(cv.match(reg));//打印结果18878553070，188，3070</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">'$1****$3'</span>));<span class="comment">//188****4070</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t3</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/[ \t\u3000]+/g</span>;<span class="comment">//找空白符  第一个是半角的空格  \s包含所有空白符  \u3000表示 全角空格的16进制Unicode编码</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">','</span>));<span class="comment">//</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t4</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> reg = <span class="regexp">/([\u4E00-\u9FA0]),([\u4E00-\u9FA0])/g</span>;  <span class="comment">// 找中文用Unicode编码表的中文范围</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">','</span>));</span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t5</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> reg = <span class="comment">//;  </span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">','</span>));</span></span><br><span class="line">            &#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="4-8-例8-常用正则"><a href="#4-8-例8-常用正则" class="headerlink" title="4.8 例8-常用正则"></a>4.8 例8-常用正则</h3><blockquote>
<p>在线演示 <a href="https://codepen.io/poetries/pen/GxzNoG" target="_blank" rel="noopener">https://codepen.io/poetries/pen/GxzNoG</a></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>常用正则解答<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>&gt;</span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t1</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/(\w)\1+/g</span>; <span class="comment">//查找连续多个字符  把aaabbccdd换成abcd这个格式</span></span></span><br><span class="line">            						/**\1 后向引用，表示表达式中，从左往右数，第一个左括号对应的括号内的内容。</span><br><span class="line">            以此类推，\2表示第二个，\0表示整个表达式**/</span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">'$1'</span>));</span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t2</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="comment">//手机号正则：</span></span></span><br><span class="line"><span class="javascript">            	<span class="comment">//1开头</span></span></span><br><span class="line"><span class="javascript">            	<span class="comment">//358第二位</span></span></span><br><span class="line"><span class="javascript">            	<span class="comment">//后九位</span></span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/1[358]\d&#123;9&#125;/g</span>; <span class="comment">// 这种情况适合在一段文本中分析出手机号</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.match(reg));<span class="comment">//分析手机号</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t3</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/^1[358]\d&#123;9&#125;$/g</span>;</span></span><br><span class="line"><span class="javascript">            	alert(reg.test(cv));<span class="comment">// test 验证手机号</span></span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t4</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/^\s+|\s+$/g</span>; <span class="comment">// | 并列 或者 去除两端空格</span></span></span><br><span class="line"><span class="javascript">            	alert(<span class="string">'--'</span>+cv.replace(reg,<span class="string">''</span>)+<span class="string">'--'</span>);</span></span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="comment">//借助正则限制在一个有限范围内 防止sql注入</span></span></span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t5</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/^[\w\u4E00-\u9FA0]&#123;4,16&#125;$/</span>; <span class="comment">// \u4E00-\u9FA0 中文对应的Unicode编码 验证由字母数字下划线中文组合的4-16位用户名 </span></span></span><br><span class="line">            	alert(reg.test(cv));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t6</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="regexp">/^[0-9a-zA-Z]\w*(\.[\w]+)*@[0-9a-zA-Z\-]+(\.[\w]+)+$/</span>;</span></span><br><span class="line">            	alert(reg.test(cv));</span><br><span class="line">            &#125;</span><br><span class="line"><span class="javascript">            <span class="function"><span class="keyword">function</span> <span class="title">t7</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cont = <span class="built_in">document</span>.getElementById(<span class="string">'cont'</span>);</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> cv = cont.value;</span></span><br><span class="line"><span class="javascript">            	<span class="keyword">var</span> reg = <span class="comment">//;</span></span></span><br><span class="line"><span class="javascript">            	alert(cv.replace(reg,<span class="string">''</span>));</span></span><br><span class="line">            &#125;</span><br><span class="line">	<span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">textarea</span> <span class="attr">id</span>=<span class="string">'cont'</span>&gt;</span><span class="tag">&lt;/<span class="name">textarea</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"1-替换相同字符串"</span> <span class="attr">onclick</span>=<span class="string">"t1();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"2-分析出手机号"</span> <span class="attr">onclick</span>=<span class="string">"t2();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"3-验证手机号"</span> <span class="attr">onclick</span>=<span class="string">"t3();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"4-去除两端空格"</span> <span class="attr">onclick</span>=<span class="string">"t4();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"5-验证由字母数字下划线中文组合的4-16位用户名"</span> <span class="attr">onclick</span>=<span class="string">"t5();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"6-验证邮箱"</span> <span class="attr">onclick</span>=<span class="string">"t6();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"7-清空script代码"</span> <span class="attr">onclick</span>=<span class="string">"t7();"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="五、总结"><a href="#五、总结" class="headerlink" title="五、总结"></a>五、总结</h2><p><img src="http://7xq6al.com1.z0.glb.clouddn.com/JavaScript%20%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F.gif" alt></p>
<h2 id="六、扩展阅读"><a href="#六、扩展阅读" class="headerlink" title="六、扩展阅读"></a>六、扩展阅读</h2><ul>
<li><a href="http://blog.poetries.top/2016/07/09/%E6%A2%B3%E7%90%86%E5%B8%B8%E7%94%A8%E7%9A%84%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">梳理常用的正则表达式</a></li>
<li><a href="http://blog.poetries.top/handbook/">一些总结</a></li>
</ul>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/03/22/Charles/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/07/22/react-hoc/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
